package cn.tedu.micro.demo.dlx;

import cn.tedu.micro.demo.client.RabbitChannelManager;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class DlxDemo {
    private static final String BIZ_EX = "biz_ex";
    private static final String BIZ_Q = "biz_q";
    private static final String DLX_EX = "dlx_ex";
    private static final String DLX_Q = "dlx_q";
    private static final String DLX_RK = "dlx_rk";

    @Test
    public void dlxDelare() throws IOException {
        Channel channel = RabbitChannelManager.newChannel();
        channel.exchangeDeclare(BIZ_EX, BuiltinExchangeType.FANOUT);
        channel.exchangeDeclare(DLX_EX, BuiltinExchangeType.DIRECT);
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange", DLX_EX);
        args.put("x-dead-letter-routing-key", DLX_RK);
        args.put("x-max-message",10);
        channel.queueDeclare(BIZ_Q, false, false, false, args);
        channel.queueDeclare(DLX_Q, false, false, false, null);
        channel.queueBind(BIZ_Q, BIZ_EX, "");
        channel.queueBind(DLX_Q, DLX_EX, DLX_RK);
    }
    @Test
    public void testNackConsme() throws IOException, InterruptedException {
        //1.声明队列
        Channel channel = RabbitChannelManager.newChannel();
        //2.绑定监听消费者
        channel.basicConsume(BIZ_Q,false,
                (consumerTag,message)->{
                    //消费入口
                    log.info("消息达到消费者");
                    //手动确认 requeue: 是否允许重新入队
                    channel.basicNack(
                            message.getEnvelope().getDeliveryTag(),
                            false,
                            false);
                },
                consumerTag->{
                });
        Thread.sleep(10000000);
    }

}
